package com.egao.common.module.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.egao.common.core.annotation.ApiPageParam;
import com.egao.common.core.annotation.OperLog;
import com.egao.common.core.web.BaseController;
import com.egao.common.core.web.JsonResult;
import com.egao.common.core.web.PageParam;
import com.egao.common.core.web.PageResult;
import com.egao.common.module.system.entity.Role;
import com.egao.common.module.system.entity.User;
import com.egao.common.module.system.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 校用户管理
 * @author pyx
 */
@Api(tags = "校用户管理")
@RestController
@RequestMapping("${api}")
public class TeacherController extends BaseController {
    @Autowired
    private UserService userService;
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private RoleService roleService;
    @Autowired
    private CollegeService collegeService;

    @PreAuthorize("hasAuthority('sys:user:list')")
    @OperLog(value = "校用户管理", desc = "分页查询")
    @ApiOperation("分页查询校用户")
    @ApiPageParam
    @GetMapping("/sys/teacher/page")
    public PageResult<User> page(HttpServletRequest request) {
        PageParam<User> pageParam = new PageParam<>(request);
        pageParam.setDefaultOrder(null, new String[]{"create_time"});
        return new PageResult<>(teacherService.listPage(pageParam), pageParam.getTotal());
    }

    @PreAuthorize("hasAuthority('sys:user:list')")
    @OperLog(value = "校用户管理", desc = "查询全部")
    @ApiOperation("查询全部校用户")
    @GetMapping("/sys/teacher")
    public JsonResult list(HttpServletRequest request) {
        PageParam<User> pageParam = new PageParam<>(request);
        List<User> records = teacherService.listAll(pageParam.getNoPageParam());
        return JsonResult.ok().setData(pageParam.sortRecords(records));
    }

    @PreAuthorize("hasAuthority('sys:user:list')")
    @OperLog(value = "校用户管理", desc = "根据id查询")
    @ApiOperation("根据id查询校用户")
    @GetMapping("/sys/teacher/{id}")
    public JsonResult get(@PathVariable("id") Integer id) {
        PageParam<User> pageParam = new PageParam<>();
        pageParam.put("userId", id);
        List<User> records = teacherService.listAll(pageParam.getNoPageParam());
        return JsonResult.ok().setData(pageParam.getOne(records));
    }

    @PreAuthorize("hasAuthority('sys:user:save')")
    @OperLog(value = "校用户管理", desc = "添加", param = false, result = true)
    @ApiOperation("添加校用户")
    @PostMapping("/sys/teacher")
    public JsonResult add(@RequestBody User teacher) {
        teacher.setState(0);
        teacher.setPassword(userService.encodePsw("0!v5EYJVNbJvZT!Do*JN"));
        if (teacherService.saveUser(teacher)) {
            return JsonResult.ok("添加成功");
        }
        return JsonResult.error("添加失败");
    }

    @PreAuthorize("hasAuthority('sys:user:update')")
    @OperLog(value = "校用户管理", desc = "修改", param = false, result = true)
    @ApiOperation("修改校用户")
    @PutMapping("/sys/teacher")
    public JsonResult update(@RequestBody User teacher) {
        teacher.setState(null);
        if (teacherService.updateUser(teacher)) {
            return JsonResult.ok("修改成功");
        }
        return JsonResult.error("修改失败");
    }

    @PreAuthorize("hasAuthority('sys:user:remove')")
    @OperLog(value = "校用户管理", desc = "删除", result = true)
    @ApiOperation("删除校用户")
    @DeleteMapping("/sys/teacher/{id}")
    public JsonResult remove(@PathVariable("id") Integer id) {
        if (teacherService.removeById(id)) {
            return JsonResult.ok("删除成功");
        }
        return JsonResult.error("删除失败");
    }

    @PreAuthorize("hasAuthority('sys:user:list')")
    @ApiOperation("查询教师与学生角色")
    @GetMapping("/sys/teacher/role")
    public JsonResult listTeacher(HttpServletRequest request) {
        PageParam<Role> pageParam = new PageParam<>(request);
        QueryWrapper<Role> wrapper = pageParam.getOrderWrapper();
        wrapper.notIn("role_id",1,4,7);
        return JsonResult.ok().setData(roleService.list(wrapper));
    }

    @PreAuthorize("hasAuthority('sys:user:list')")
    @ApiOperation("查询全部学院")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "xl", value = "学历", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "keyword", value = "学院名称或编码", required = false, dataType = "String", paramType = "query")
    })
    @GetMapping("/sys/teacher/college")
    public JsonResult list(Integer xl, String keyword) {
        return JsonResult.ok().setData(collegeService.listAll( xl, keyword));
    }
}
